<?

class SMMessage{
    // message types
    const PHP_ERROR = 1;
    const PHP_WARNING = 2;
    const PHP_NOTICE = 3;
    const PHP_UNKNOWN = 4;
    const PHP_STRICT = 5;
    
    // class vars
    public $message;
    public $message_type;
    public $message_line;
    public $message_file;
    public $php_self;

    public function  __construct($errno, $errstr, $errfile, $errline) {
        $this->setMessage($errno, $errstr, $errfile, $errline);
        $this->php_self = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    }

    private function setMessage($errno, $errstr, $errfile, $errline){
        switch ($errno) {
            case E_NOTICE:
            case E_USER_NOTICE:
                $errors = "Notice";
                $message_type = self::PHP_NOTICE;
                break;
            case E_WARNING:
            case E_USER_WARNING:
                $errors = "Warning";
                $message_type = self::PHP_WARNING;
                break;
            case E_ERROR:
            case E_USER_ERROR:
                $errors = "Fatal Error";
                $message_type = self::PHP_ERROR;
                break;
            case E_STRICT:
                $errors = "Strict";
                $message_type = self::PHP_STRICT;
                break;
            default:
                $errors = "Unknown";
                $message_type = self::PHP_UNKNOWN;
                break;
        }

        $this->message_line = $errline;
        $this->message_file = $errfile;
        $this->message = strip_tags($errstr);
        $this->message_type = $message_type;

        /*error_log("vars: " . $this->message);
        die();*/
    }

    public function getMessage(){
        return $this->message;
    }

    public function getMessageType(){
        return $this->message_type;
    }

    public function getMessageLine(){
        return $this->message_line;
    }

    public function getMessageFile(){
        return $this->message_file;
    }

    public function getPHPSelf(){
        return $this->php_self;
    }
}

class SMMessagePackage {
    public $sm_messages=array(); // array of SMMesage objects
    private $transmitter_base_url=null; // stores the url of the generated request
    private $application_key=null;
    private static $instance=null;

    public function  __construct($application_key, $transmitter_base_url) {
        $this->setApplicationKey($application_key);
        $this->setTransmitterBaseUrl($transmitter_base_url);
    }

    public static function getInstance($application_key, $transmitter_base_url){
        if(self::$instance === null){
            self::$instance = new SMMessagePackage($application_key, $transmitter_base_url);
        }

        return self::$instance;
    }

    public function setApplicationKey($application_key=null){
        if($application_key){
            $this->application_key = $application_key;
        }else{
            trigger_error(__CLASS__ . ": application must pass an application key to the SMMessagePackage constructor", E_USER_ERROR);
            die();
        }

        return true;
    }

    public function addSMMessage(SMMessage $obj){
        $this->sm_messages[] = $obj;
    }

    public function setTransmitterBaseUrl($transmitter_base_url=null){
        if($transmitter_base_url){
            $this->transmitter_base_url = $transmitter_base_url;
        }else{
            trigger_error(__CLASS__ . ": application must pass a transmitter base url to the SMMessagePackage constructor", E_USER_ERROR);
            die();
        }

        return true;
    }

    public function getSMMessages(){
        return $this->sm_messages;
    }

    public function getTransmitterBaseUrl(){
        return $this->transmitter_base_url;
    }

    public function getApplicationKey(){
        return $this->application_key;
    }
}
?>